$(function () {
    tabledata();
    layuiEdit();
    createTime_search();
});
// 加载列表
function tabledata() {
    layui.use('table', function () {
        var form = layui.form;
        var table = layui.table;
        table.render({
            elem: "#commentDataTable",
            page: true,
            even: true,
            limits: [10, 20, 30],
            limit: 10,
            url: '/comment/showList',
            toolbar: '#commentDataTable',
            id: 'dataTable',
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field: 'commentId', title: '评论编号'},
                {field: 'commentAgent', title: '浏览器标识'},
                {field: 'commentAuthor', title: '评论人'},
                {field: 'commentAuthorAvatarMd5', title: '头像(引用地址)'},
                {field: 'commentAuthorEmail', title: '评论者邮箱'},
                {field: 'commentAuthorIp', title: 'ip'},
                {field: 'commentAuthorUrl', title: '地址'},
                {field: 'commentContent', title: '评论内容'},
                {field: 'commentDate', title: '评论日期', templet: function (d) {
                    console.log(d);
                        return fmt(d.commentDate);
                    }},
                {field: 'commentParent', title: '评论层楼主'},
                {field: 'commentStatus', title: '评论状态'},
                {field: 'isAdmin', title: '是否管理'},
                {field: 'postId', title: '串号'},
                {field: 'logicDel', title: '操作', toolbar: '#commentBar'}
            ]],
            request: {
                page: 'page' //页码的参数名称，默认：page
                , pageSize: 'limit' //每页数据量的参数名，默认：limit
            }
        });
        var $ = layui.$,
            active = {
                reload: function () {
                    var createTime = $("#comment_createTime").val(),
                        title = $("#comment_Auctitle").val();

                    table.reload("dataTable", {
                        url: '/comment/showList',
                        where: {
                            createTime: createTime,
                            title: title
                        },
                        page: {
                            page: 1
                        }
                    });
                }
            };
        $("#comment_search").on("click", function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : "";
        })
    });

}
function fmt(timestamp) {

    // var year = now.getFullYear();
    // var month = now.getMonth() + 1;
    // var date = now.getDate();
    // var hour = now.getHours();
    // var minute = now.getMinutes();
    // var second = now.getSeconds();
    // return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;

    //13位时间戳转换
    var date = new Date(timestamp / 1);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + " " + h + ":" + minute + ":" + second;
//10位时间戳转换
//             function timestampToTime2(timestamp) {
//     var date = new Date(timestamp * 1000);
//     var y = date.getFullYear();
//     var m = date.getMonth() + 1;
//     m = m < 10 ? ('' + m) : m;
//     var d = date.getDate();
//     d = d < 10 ? ('0' + d) : d;
//     var h = date.getHours();
//     h = h < 10 ? ('0' + h) : h;
//     var minute = date.getMinutes();
//     var second = date.getSeconds();
//     minute = minute < 10 ? ('0' + minute) : minute;
//     second = second < 10 ? ('0' + second) : second;
//     return y + '-' + m + '-' + d + " " + h + ":" + minute + ":" + second;
//             }
}
//条件搜索框之时间
function createTime_search() {
    layui.use('laydate', function(){
        var laydate = layui.laydate;
        laydate.render({
            elem: '#comment_createTime'
            , type: 'datetime'
            , range: true
        });
    });
}

//编辑
function layuiEdit() {
    layui.use('table', function () {
        var thas = this;
        var form = layui.form;
        var table = layui.table;
        //监听表格复选框选择
        table.on('checkbox(dataTable)', function (obj) {
        });
        //监听工具条
        table.on('tool(dataTable)', function (obj) {
            var _self = this;
            var data = obj.data;
            //console.log(data);
            //表格查看
            if (obj.event === 'detail') {
                layer.open({
                    type: 1,
                    title: "查看留言信息",
                    maxmin: true,
                    offset: '100px',
                    area: ['400px', '850px'],
                    content: $("#win_show_news"),// iframe的url
                    success: function (layero, index) {
                        //机械性赋值
                        $("#show_news_title").text(data.title);
                        $("#show_news_img").attr("src", data.newsImg);
                        $(document).ready(function () {
                            $("#show_news_content").html(data.content);
                        });
                        layer.iframeAuto(index);
                        layer.full(index);
                    }
                });

            }
            //表格删除
            else if (obj.event === 'del') {
                layer.confirm('真的删除这条数据么？', function (index) {
                    $.ajax({
                        type: "delete",
                        url: "/news/delete/" + data.newsId,
                        data: null,
                        dataType: "json",
                        async: false,
                        success: function (data) {
                            //console.log(data);
                            if (data.data == 1) {
                                window.location.href = "/news/";
                                layer.msg(data.msg, {time: 1 * 1000});
                            } else {
                                layer.msg(data.msg, {time: 1 * 1000});
                            }
                        }
                    });
                    layer.close(index);
                });
            }
            //表格修改
            else if (obj.event === 'edit') {
                var acu = JSON.stringify(data);
                var indexs = layer.open({
                    type: 1,
                    title: "添加信息",
                    maxmin: true,
                    offset: '100px',
                    area: ['400px', '800px'],
                    content: $("#win-news-update"),// iframe的url
                    success: function (layero, index) {
                        layui.use('layedit', function () {
                            console.log(acu);
                            var layedit = layui.layedit
                                , $ = layui.jquery;
                            layedit.set({
                                uploadImage: {
                                    url: 'http://crawler_upload.6ack.com/upload/layedit' //接口url
                                    , type: 'POST' //默认post
                                }
                            });
                            $("#news_content_update").text(data.content);
                            $("#news_title_update").val(data.title);
                            $("#news_img_span_update").text(data.newsImg);
                            $("#newsId").val(data.newsId);
                            //构建一个默认的编辑器
                            var index = layedit.build('news_content_update');

                            //编辑器外部操作
                            var active = {
                                content: function () {
                                    console.log(layedit.getContent(index)); //获取编辑器内容
                                }
                                , text: function () {
                                    alert(layedit.getText(index)); //获取编辑器纯文本内容
                                }
                                , selection: function () {
                                    alert(layedit.getSelection(index));
                                }
                            };
                            $('#news_update').on('click', function () {
                                var content = layedit.getContent(index);
                                var title = $("#news_title_update").val();
                                var imgspan = $("#news_img_span_update").text();
                                var newsId = $("#newsId").val();
                                if (title == null || title === "") {
                                    layer.msg("请填写公告缩略内容！");
                                    return;
                                }
                                if (content == null || content === "") {
                                    layer.msg("请填写文本框内容！");
                                    return;
                                }
                                datas = {};
                                datas.content = content;
                                datas.title = title;
                                datas.newsImg = imgspan;
                                datas.newsId = newsId;
                                //提交数据
                                $.ajax({
                                    type: "PUT",
                                    url: "/news/update",
                                    data: JSON.stringify(datas),
                                    contentType: "application/json; charset=utf-8",
                                    dataType: 'json',//预期服务器返回的数据类型
                                    beforeSend: function () {
                                        //让提交按钮失效
                                        $('#news_update').prop('disabled', "true");
                                    },
                                    success: function (data) {
                                        layui.use('layer', function () {
                                            var layer = layui.layer;
                                            layer.open({
                                                content: "操作成功！",//提示信息
                                                success: function () {
                                                    layer.msg(data.msg, {time: 1 * 1000}, function () {
                                                        window.location.href = "/news/";
                                                    });
                                                }
                                            });
                                            var index = layedit.build('news_content_update');
                                            $("#news_res_update").click();
                                        });

                                    },
                                    complete: function () {
                                        $('#sbbtn').prop('disabled', false);

                                    }

                                });
                            });
                        });
                        layer.iframeAuto(index);
                        layer.full(index);
                    }

                });
            }
        });
    });
}